﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using  VAdvantage.UIEvent;
using System.Windows.Media.Imaging;
using VAdvantage.Utility;

namespace VAdvantage.Editor
{
    public class ContextMenuStrip : ContextMenu, IEventLitnerer
    {


        private const long serialVersionUID = 8172397145177408454L;
        private const String EVENT_ATTRIBUTE = "EVENT";
        public const String ZOOM_EVENT = "ZOOM";
        public const String REQUERY_EVENT = "REQUERY";
        public const String PREFERENCE_EVENT = "VALUE_PREFERENCE";
        public const String NEW_EVENT = "NEW_RECORD";
        public const String UPDATE_EVENT = "UPDATE_RECORD";
        public const String DELETE_EVENT = "DELETE_RECORD";

        private bool newEnabled = false;
        private bool updateEnabled = false;
        private bool zoomEnabled = false;
        private bool requeryEnabled = false;
        private bool preferencesEnabled = false;
        private bool deleteEnabled = false;

        private VMenuItem zoomItem;
        private VMenuItem requeryItem;
        private VMenuItem prefItem;
        private VMenuItem newItem;
        private VMenuItem updateItem;
        private VMenuItem deleteItem;

        public MenuItem ZoomItem
        {
            get { return zoomItem; }
        }

        private List<IContextMenuListener> menuListeners = new List<IContextMenuListener>();

        private IContextMenuListener _listnerer = null;

        public ContextMenuStrip()
        {
        }

        public ContextMenuStrip(bool zoom, bool requery, bool preferences) :
            this(zoom, requery, preferences, false, false)
        {
        }

        public ContextMenuStrip(bool zoom, bool requery, bool preferences, bool newRecord)
            : this(zoom, requery, preferences, newRecord, false)
        {
        }

        public ContextMenuStrip(bool zoom, bool requery, bool preferences, bool newRecord, bool updateRecord):this(zoom,requery,preferences,newRecord,updateRecord,false)
        {
        }

        public ContextMenuStrip(bool zoom, bool requery, bool preferences, bool newRecord, bool updateRecord,bool deleteRecord)
        {
            this.zoomEnabled = zoom;
            this.requeryEnabled = requery;
            this.preferencesEnabled = preferences;
            this.newEnabled = newRecord;
            this.updateEnabled = updateRecord;
            this.deleteEnabled = deleteRecord;
            Init();
        }

        public bool IsZoomEnabled()
        {
            return zoomEnabled;
        }

        private void Init()
        {
            if (zoomEnabled)
            {
                zoomItem = new VMenuItem();
                zoomItem.ActionName = ZOOM_EVENT;
                //zoomItem.Command  = ZOOM_EVENT;
                zoomItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Zoom");
                zoomItem.Icon = GetIcon("Zoom16");

                zoomItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, zoomItem));
                };

                this.Items.Add(zoomItem);
            }

            if (requeryEnabled)
            {
                requeryItem = new VMenuItem();
                requeryItem.ActionName = REQUERY_EVENT;
                requeryItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Refresh");
                requeryItem.Icon = GetIcon("Refresh16");
                requeryItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, requeryItem));
                };
                this.Items.Add(requeryItem);
            }

            if (preferencesEnabled)
            {
                prefItem = new VMenuItem();
                prefItem.ActionName = PREFERENCE_EVENT;
                prefItem.Header = "Value Preference";
                prefItem.Icon = GetIcon("VPreference16");
                prefItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, prefItem));
                };
                this.Items.Add(prefItem);
            }

            if (newEnabled)
            {
                newItem = new VMenuItem();
                newItem.ActionName = NEW_EVENT;
                newItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "New");
                newItem.Icon = GetIcon("New16");
                newItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, newItem));
                };
                this.Items.Add(newItem);
            }

            if (updateEnabled)
            {
                updateItem = new VMenuItem();
                updateItem.ActionName = UPDATE_EVENT;
                updateItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Update");
                updateItem.Icon = GetIcon("InfoBPartner16");
                updateItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, updateItem));
                };
                this.Items.Add(updateItem);
            }

            if (deleteEnabled)
            {

                deleteItem = new VMenuItem();
                deleteItem.ActionName = DELETE_EVENT;
                deleteItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Delete");
                deleteItem.Icon = GetIcon("Delete16");
                deleteItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, deleteItem));
                };
                this.Items.Add(deleteItem);

            }
        }

        public void AddMenuListener(IContextMenuListener listener)
        {
            if (!menuListeners.Contains(listener))
                menuListeners.Add(listener);
        }

        public void AddContextListener(IContextMenuListener listnerer)
        {
            _listnerer = listnerer;
        }

        public void OnEvent(Event e)
        {
            String evt = ((VMenuItem)e.GetTarget()).ActionName;

            if (evt != null)
            {
                if (_listnerer != null)
                {
                    _listnerer.OnMenu(new ContextMenuEvent(evt));
                }
                else
                {

                    ContextMenuEvent menuEvent = new ContextMenuEvent(evt);

                    IContextMenuListener[] listeners = new IContextMenuListener[0];
                    listeners = menuListeners.ToArray();
                    foreach (IContextMenuListener listener in listeners)
                    {
                        listener.OnMenu(menuEvent);
                    }
                }
            }
        }

        public  object GetIcon(string imageName)
        {
            Image img = new Image();
            img.Source = Envs.LoadImageSource(imageName+".jpg");
            return img;
        }

        internal void AddNewItem()
        {
            if (this.newEnabled)
                return;
            newItem = new VMenuItem();
            newItem.ActionName = NEW_EVENT;
            newItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "New");
            newItem.Icon = GetIcon("New16");
            newItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, newItem));
            };
            this.Items.Add(newItem);

            this.newEnabled = true;
        }

        internal void AddUpdateItem()
        {
            if (this.updateEnabled)
                return;
            updateItem = new VMenuItem();
            updateItem.ActionName = UPDATE_EVENT;
            updateItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Update");
            updateItem.Icon = GetIcon("InfoBPartner16");
            updateItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, updateItem));
            };
            
            this.Items.Add(updateItem);

            this.updateEnabled = true;
        }

        internal void AddDeleteItem()
        {
            if (this.deleteEnabled)
                return;
            deleteItem = new VMenuItem();
            deleteItem.ActionName = DELETE_EVENT;
            deleteItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Delete");
            deleteItem.Icon = GetIcon("Delete16");
            deleteItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, deleteItem));
            };
            this.Items.Add(deleteItem);
            this.deleteEnabled = true;
        }

        internal void AddZoomItem()
        {
            if (this.zoomEnabled)
                return;
            zoomItem = new VMenuItem();
            zoomItem.ActionName = ZOOM_EVENT;
            zoomItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Zoom");
            zoomItem.Icon = GetIcon("Zoom16");
            zoomItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, zoomItem));
            };
            this.Items.Add(zoomItem);
            this.zoomEnabled = true;
        }

        internal void AddRequeryItem()
        {
            if (this.requeryEnabled)
                return;
            requeryItem = new VMenuItem();
            requeryItem.ActionName = REQUERY_EVENT;
            requeryItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Requery");
            requeryItem.Icon = GetIcon("Refresh16");
            requeryItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, requeryItem));
            };
            this.Items.Add(requeryItem);
            this.requeryEnabled = true;
        }

        internal void AddPreferenceItem()
        {
            if (this.preferencesEnabled)
                return;
            prefItem = new VMenuItem();
            prefItem.ActionName = PREFERENCE_EVENT;
            prefItem.Header = Utility.Msg.GetMsg(Envs.GetCtx(), "Preference");
            prefItem.Icon = GetIcon("Preference16");
            prefItem.Click += delegate
            {
                OnEvent(new Event(Events.ON_CLICK, prefItem));
            };
            this.Items.Add(prefItem);
            this.preferencesEnabled = true;
        }

    }

    public class VMenuItem : MenuItem
    {
        public string ActionName
        {
            get;
            set;
        }
    }
}
